package app.qrcode.browser.todoapp.fragments.update

import android.app.Dialog
import android.app.TimePickerDialog
import android.os.Bundle
import android.text.format.DateFormat
import android.text.format.DateFormat.is24HourFormat
import android.util.Log
import android.widget.TimePicker
import androidx.fragment.app.DialogFragment
import app.qrcode.browser.todoapp.utils.dateFormater
import app.qrcode.browser.todoapp.utils.timeFormater
import java.util.*

/**
 * Created by YiGo on 2022/9/19.
 */
class TimePickerFragment : DialogFragment(), TimePickerDialog.OnTimeSetListener {

    lateinit var timeChosen: TimeChosen

    interface TimeChosen {

        fun setTime(str: String)

    }

    @JvmName("setTimeChosen1")
    fun setTimeChosen(timeChosen: TimeChosen) {
        this.timeChosen = timeChosen
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        // Use the current time as the default values for the picker
        val c = Calendar.getInstance()
        val hour = c.get(Calendar.HOUR_OF_DAY)
        val minute = c.get(Calendar.MINUTE)

        // Create a new instance of TimePickerDialog and return it
        return TimePickerDialog(activity, this, hour, minute, is24HourFormat(activity))
    }

    override fun onTimeSet(view: TimePicker, hourOfDay: Int, minute: Int) {
        // Do something with the time chosen by the user
//        val timeStr: String = timeFormater.format(System.currentTimeMillis())
        var hourStr = if (hourOfDay.toString().length < 2) "0$hourOfDay" else hourOfDay.toString()
        var minuteStr = if (minute.toString().length < 2) "0$minute" else minute.toString()
        timeChosen.setTime("$hourStr:$minuteStr")
    }
}